home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / lynx-2.4 / WWW / Library / Implementation / HTHistory.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-28  |  3.8 KB  |  158 lines

  1. #include "HTUtils.h"
  2. #include "tcp.h"        /* for standard io */
  3.  
  4. #include "HTHistory.h"
  5.  
  6. #include "LYLeaks.h"
  7.  
  8. static HTList * history;    /* List of visited anchors */
  9.  
  10.  
  11. /*                Navigation
  12. **                ==========
  13. */
  14.  
  15. /*        Record the jump to an anchor
  16. **        ----------------------------
  17. */
  18.  
  19. void HTHistory_record
  20.   ARGS1 (HTAnchor *,destination)
  21. {
  22.   if (destination) {
  23.     if (! history)
  24.       history = HTList_new();
  25.     HTList_addObject (history, destination);
  26.   }
  27. }
  28.  
  29. /*        Go back in history (find the last visited node)
  30. **        ------------------
  31. */
  32.  
  33. HTAnchor * HTHistory_backtrack
  34.   NOARGS  /* FIXME: Should we add a `sticky' option ? */
  35. {
  36.   if (HTHistory_canBacktrack())
  37.     HTList_removeLastObject (history);
  38.   return HTList_lastObject (history);  /* is Home if can't backtrack */
  39. }
  40.  
  41. BOOL HTHistory_canBacktrack
  42.   NOARGS
  43. {
  44.   return (HTList_objectAt (history, 1) != NULL);
  45. }
  46.  
  47. /*        Browse through references in the same parent node
  48. **        -------------------------------------------------
  49. **
  50. **    Take the n-th child's link after or before the one we took to get here.
  51. **    Positive offset means go towards most recently added children.
  52. */
  53.  
  54. HTAnchor * HTHistory_moveBy
  55.  ARGS1 (int,offset)
  56. {
  57.   HTAnchor * last = HTList_objectAt (history, 1);
  58.   if (! last)
  59.     return NULL;  /* No last visited node */
  60.   if (last != (HTAnchor *) last->parent) {  /* Was a child */
  61.     HTList * kids = last->parent->children;
  62.     int i = HTList_indexOf (kids, last); 
  63.     HTAnchor * nextOne = HTList_objectAt (kids, i - offset);
  64.     if (nextOne) {
  65.       HTAnchor * destination = HTAnchor_followMainLink (nextOne);
  66.       if (destination) {
  67.     HTList_removeLastObject (history);
  68.     HTList_removeLastObject (history);
  69.     HTList_addObject (history, nextOne);
  70.     HTList_addObject (history, destination);
  71.       }
  72.       return destination;
  73.     } else {
  74.       if (TRACE) fprintf(stderr, 
  75.               "HTHistory_moveBy: offset by %+d goes out of list %p.\n",
  76.         offset, (void*)kids);
  77.       return NULL;
  78.     }
  79.   } else {  /* Was a parent */
  80.     return NULL;  /* FIXME we could possibly follow the next link... */
  81.   }
  82. }
  83.  
  84. BOOL HTHistory_canMoveBy
  85.  ARGS1 (int,offset)
  86. {
  87.   HTAnchor * last = HTList_objectAt (history, 1);
  88.   if (! last)
  89.     return NO;  /* No last visited node */
  90.   if (last != (HTAnchor *) last->parent) {  /* Was a child */
  91.     HTList * kids = last->parent->children;
  92.     int i = HTList_indexOf (kids, last); 
  93.     return (HTList_objectAt (kids, i - offset) != NULL);
  94.   } else {  /* Was a parent */
  95.     return NO;  /* FIXME we could possibly follow the next link... */
  96.   }
  97. }
  98.  
  99.  
  100. /*                Retrieval
  101. **                =========
  102. */
  103.  
  104. /*        Read numbered visited anchor (1 is the oldest)
  105. **        ----------------------------
  106. */
  107.  
  108. HTAnchor * HTHistory_read
  109.   ARGS1 (int,number)
  110. {
  111.   return HTList_objectAt (history, HTList_count (history) - number);
  112. }
  113.  
  114.  
  115. /*        Recall numbered visited anchor (1 is the oldest)
  116. **        ------------------------------
  117. **    This reads the anchor and stores it again in the list, except if last.
  118. */
  119.  
  120. HTAnchor * HTHistory_recall
  121.   ARGS1 (int,number)
  122. {
  123.   HTAnchor * destination =
  124.     HTList_objectAt (history, HTList_count (history) - number);
  125.   if (destination && destination != HTList_lastObject (history))
  126.     HTList_addObject (history, destination);
  127.   return destination;
  128. }
  129.  
  130. /*        Number of Anchors stored
  131. **        ------------------------
  132. **
  133. **    This is needed in order to check the validity of certain commands
  134. **    for menus, etc.
  135. (not needed for now. Use canBacktrack, etc.)
  136. int HTHistory_count
  137.   NOARGS
  138. {
  139.   return HTList_count (history);
  140. }
  141. */
  142.  
  143. /*        Change last history entry
  144. **        -------------------------
  145. **
  146. **    Sometimes we load a node by one anchor but leave by a different
  147. **    one, and it is the one we left from which we want to remember.
  148. */
  149.  
  150. void HTHistory_leavingFrom
  151.   ARGS1 (HTAnchor *,anchor)
  152. {
  153.   if (HTList_removeLastObject (history))
  154.     HTList_addObject (history, anchor);
  155.   else
  156.     if (TRACE) fprintf(stderr, "HTHistory_leavingFrom: empty history !\n");
  157. }
  158.